from twisted.internet import protocol, reactor
from twisted.python import log
from twisted.python.logfile import DailyLogFile
from caster_protocol import CasterProtocol
import sys
import update_logic
import database
players = {}

log.startLogging(sys.stdout)
log.addObserver(log.FileLogObserver(open("./log/caster.log",'a')).emit)
database.create()
class GameFactory(protocol.ServerFactory):

    protocol = CasterProtocol
    
    def __init__(self):
        self.players = players
        update_logic.init(self)
        
    def get_players(self, loc_id):
        ''' returns all players for specified location '''
        return [p for p in self.players.values() if p.loc_id == loc_id]
        
    def send_updates(self, loc_id):
        print "TODO: Every player in loc:{} receive an update of map".format(loc_id)


reactor.listenTCP(8888, GameFactory()) #@UndefinedVariable
reactor.run() #@UndefinedVariable
